package com.cskaoyan.service;
import com.cskaoyan.bean.data.KeywordData;
import com.cskaoyan.domain.*;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    MarketKeywordMapper keywordMapper;

    @Autowired
    MarketSearchHistoryMapper historyMapper;

    @Override
    public Map<String, List> getIndex() {
        //创建一个空的Map对象
        Map map = new HashMap<>();

        //查询默认关键词列表
        MarketKeywordExample defaultExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria defaultCriteria = defaultExample.createCriteria();
        defaultCriteria.andIsDefaultEqualTo(true).andIsDefaultEqualTo(false);
        List<MarketKeyword> dafaultkeywordList = keywordMapper.selectByExample(defaultExample);

        //查询搜索历史列表
        Subject subject = SecurityUtils.getSubject();
        List<KeywordData> historyKeywordList = new ArrayList<>();
        //是否被验证
        if (subject.isAuthenticated()) {
            //如果用户已经登录，查询对应用户的搜索历史
            MarketUser principal = (MarketUser) subject.getPrincipal();
            Integer userId = principal.getId();
            MarketSearchHistoryExample historyExample = new MarketSearchHistoryExample();
            MarketSearchHistoryExample.Criteria historyCriteria = historyExample.createCriteria();
            historyCriteria.andUserIdEqualTo(userId).andDeletedEqualTo(false);

            List<MarketSearchHistory> historyList = historyMapper.selectByExample(historyExample);
            for (MarketSearchHistory searchHistory : historyList) {
                KeywordData keywordData = new KeywordData(searchHistory.getKeyword());
                historyKeywordList.add(keywordData);
            }
        } else {
            //如果用户未登录，搜索历史为空
            historyKeywordList = null;
        }
        //查询热门关键词列表
        MarketKeywordExample hotKeywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria hotCriteria = hotKeywordExample.createCriteria();
        hotCriteria.andIsHotEqualTo(true).andDeletedEqualTo(false);
        List<MarketKeyword> hotKeywordList = keywordMapper.selectByExample(hotKeywordExample);

        //将查询的结果放入Map中
        map.put("defaultKeyword", dafaultkeywordList);
        map.put("historyKeywordList", historyKeywordList);
        map.put("hotKeywordList", hotKeywordList);
        // 返回 Map 对象
        return map;
    }

    @Override
    public List<String> getHelper(String keyword) {
        /*String str = keyword;
        String[] arr = str.split("(?<=.)");
        System.out.println(Arrays.toString(arr));*/

        /*List<String> listKeyword = new ArrayList<>();

        MarketKeywordExample keywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andKeywordLike(keyword);

        List<MarketKeyword> list = keywordMapper.selectByExample(keywordExample);

        for (MarketKeyword marketKeyword : list) {
            listKeyword.add(marketKeyword.getKeyword());
        }*/
        MarketKeywordExample keywordExample = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = keywordExample.createCriteria();
        criteria.andKeywordLike("%" + keyword + "%");

        List<MarketKeyword> keywordList = keywordMapper.selectByExampleSelective(keywordExample);
        List<String> likeWordList = new ArrayList<>();
        for (MarketKeyword marketKeyword : keywordList) {
            likeWordList.add(marketKeyword.getKeyword());
        }
        return likeWordList;
    }

    @Override
    @Transactional
    public int clearSearchHistory() {
        //创建一个MarketSearchHistoryExample 对象，用于构建搜索历史记录的查询条件。
        MarketSearchHistoryExample historyExample = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = historyExample.createCriteria();
        //使用 SecurityUtils.getSubject() 方法来获取当前登录的用户信息。
        Subject subject = SecurityUtils.getSubject();


        //判断用户是否已经通过身份验证，如果用户已经通过身份验证，
        // 就可以获取到用户的主体信息，即 MarketUser 对象，并从中获取用户的 ID。
        Integer userId = null;
        if (subject.isAuthenticated()) {
            MarketUser principal = (MarketUser) subject.getPrincipal();
            userId = principal.getId();
        }
        //将用户 ID 和 deleted 属性加入查询条件中，以获取对应用户的未删除的搜索历史记录
        criteria.andUserIdEqualTo(userId).andDeletedEqualTo(false);

       //逻辑删除
        List<MarketSearchHistory> marketSearchHistories = historyMapper.selectByExample(historyExample);

        for (MarketSearchHistory marketSearchHistory : marketSearchHistories) {
            marketSearchHistory.setDeleted(true);
            historyMapper.updateByPrimaryKeySelective(marketSearchHistory);
        }
        return 0;
    }

}
